#include "bookmsg.h"
#include "ui_bookmsg.h"

BookMsg::BookMsg(QString name, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BookMsg)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_StyledBackground, true);
    ui->label->setAttribute(Qt::WA_StyledBackground, true);
    this->setObjectName("widget");
    this->setStyleSheet("#widget{background-image: url(:/res/beijing.jpg);}");

    book = Sqlite::Get()->Select_book(name);

    //move(0, 40);
    //border-image:url(:/res/bali.png);
    //border-image:url(:/res/bali.png)
    QString img = QString("border-image:url(:/res/%1);").arg(book.img.simplified());
    qDebug() << "path = " << img;
    ui->label->setStyleSheet(img);//:/res/bali.png
    ui->name_label->setText(QString("《%1》").arg(name));
    ui->num_lineEdit->setText(QString::number(book.num));
    ui->sell_lineEdit->setText(QString::number(book.sell));
    //ui->num_label->setText(QString("在馆书籍（本）：%1").arg(num));
    //ui->sell_label->setText(QString("借阅所需积分：%1").arg(sell));

    ui->num_lineEdit->installEventFilter(this);
    ui->num_lineEdit->setFocusPolicy(Qt::ClickFocus);

    ui->sell_lineEdit->installEventFilter(this);
    ui->sell_lineEdit->setFocusPolicy(Qt::ClickFocus);

    key = new KeyBoard(this);
    key->setGeometry(10, 290, 780, 200);
    key->hide();
    key->init_number();
    connect(key, &KeyBoard::itemClick, this, &BookMsg::keyPress);
}

BookMsg::~BookMsg()
{
    delete ui;
}

void BookMsg::on_pushButton_clicked()
{
    QString num = ui->num_lineEdit->text();
    QString sell = ui->sell_lineEdit->text();
    if(num.isEmpty() || sell.isEmpty())
    {
        QMessageBox::about(this, "tips", "输入不能为空");
        return ;
    }
    else
    {
        book.num = num.toInt();
        book.sell = sell.toInt();
        Sqlite::Get()->Update_book(book);
        QMessageBox::about(this, "tips", "修改成功！！");
    }

    this->close();
}


void BookMsg::keyPress(QString ch)
{
    qDebug() << ch;
    if(ch == "A/a")
    {
        key->init_larget();
    }
    else if(ch == "a/A")
    {
        key->init_small();
    }
    else if(ch == "123L" || ch == "123l" || ch == "123")
    {
        key->init_number();
    }/*
    else if(ch == "&")
    {
        if(nameFocuse)
            ui->name_lineEdit->insert("&");
        else
            ui->passwd_lineEdit->insert("&");
    }*/
    else if(ch == "->")
    {
        deleteBtn_click();
    }
    else
    {
        QLineEdit *lineEdit = nullptr;
        switch (choose)
        {
            case NUM_ROLE:
            {
                lineEdit = ui->num_lineEdit;
                break;
            }
            case SELL_ROLE:
            {
                lineEdit = ui->sell_lineEdit;
                break;
            }
            default:
                break;
        }
        if(lineEdit != nullptr)
        {
            lineEdit->insert(ch);
        }
    }
}

bool BookMsg::eventFilter(QObject * watched, QEvent * event)
{
    if (ui->num_lineEdit == watched)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            qDebug() << "MouseButtonPress name_lineEdit";
            choose = NUM_ROLE;
            key->show();
        }
    }
    else if(ui->sell_lineEdit == watched)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            qDebug() << "MouseButtonPress passwd_lineEdit";
            choose = SELL_ROLE;
            key->init_number();
            key->show();
        }
    }

    return QWidget::eventFilter(watched, event);
}

void BookMsg::mouseReleaseEvent(QMouseEvent *)
{
    key->hide();
}

void BookMsg::deleteBtn_click()
{
    QLineEdit *lineEdit = nullptr;
    QString text = "";
    int pos = -1;
    switch (choose)
    {
        case NUM_ROLE:
        {
            lineEdit = ui->num_lineEdit;
            break;
        }
        case SELL_ROLE:
        {
            lineEdit = ui->sell_lineEdit;
            break;
        }
        default:
            break;
    }
    if(lineEdit != nullptr)
    {
        text = lineEdit->text();
        if(text.isEmpty())
        {
            lineEdit->setFocus();
            return ;
        }
        pos = lineEdit->cursorPosition();

        if(pos <= text.size() && pos != 0)
        {
            text.remove(pos-1, 1);
            lineEdit->setText(text);
            lineEdit->setCursorPosition(pos-1);
        }
    }
}
